<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html lang="en">
    <head>        
        <!-- META SECTION -->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        
        <link rel="icon" href="favicon.ico" type="image/x-icon" />
        <!-- END META SECTION -->
        
        <!-- CSS INCLUDE -->        
        <link rel="stylesheet" href="${ctx}/rs/css/bootstrap-table.css" />
        <link rel="stylesheet" href="${ctx}/rs/css/main.css" />
        <link id="theme" rel="stylesheet" href="${ctx}/rs/joli/css/theme-default.css" />
        
        <!-- EOF CSS INCLUDE -->       
        <style type="text/css">
            #m-info {
                position: absolute;
                top: 0;left: 0;
                /*background-color: rgba(0,0,0,.7);*/
                width: 350px;height: 400px;
                /*color: #fff;
                padding:10px;
                font-size: 12px;*/
                display: none;
            }
            #m-info img { width:100%;}
        </style>                             
    </head>
<body>
    
    <!-- START PAGE CONTAINER -->
        <div class="page-container">
            <!-- 引入左菜单部分 -->
            <jsp:include page="../joli/menu.jsp"></jsp:include>
            
            <!-- START PAGE CONTENT -->
            <div class="page-content">
                
                <jsp:include page="../joli/top.jsp"></jsp:include>
                
                <!-- START BREADCRUMB -->
                <ul class="breadcrumb">
                    <li><a href="#">套餐管理</a></li>                    
                    <li class="active">套餐列表</li>
                </ul>
                <!-- END BREADCRUMB -->   
                
                
                <!-- PAGE CONTENT WRAPPER -->
                <div class="page-content-wrap">
                    <div class="row">
                        <div class="col-xs-12">
                            <div id="toolbar">
                                <!-- <shiro:hasPermission name="aduser:pass">
                                    <button class="btn btn-info btn-sm srt-btn icon-adds" onclick="pass()">通过</button>
                                </shiro:hasPermission> -->
                                <button class="btn btn-info btn-sm srt-btn icon-send" onclick="add()">新建套餐</button>
                                <div class="fr pb10">
                                 状态&nbsp;:<select class="srt-put1 mr10 widput1" id="comboStatus" onchange="refresh()">
                                    <option value="">全部</option>
                                    <option value="1">执行中</option>
                                    <option value="2">关闭</option>
                                    <option value="0">草稿</option>
                                </select>
                                     套餐名称：
                                  <input type="text" class="srt-put1 mr10 widput1" placeholder="搜索套餐名" id="comboTitle" name="comboTitle"/>
                                  <button class="btn btn-primary btn-sm srt-btn active" onclick="refresh()">搜索</button>
                                </div>
                            </div>
                            
                            <table id="table" cellpadding="0" cellspacing="0" data-toggle="table" data-toolbar="#toolbar"
                                data-url="${ctx}/TblComboInfo/page.htm"
                                data-cache="false"
                                data-pagination="true" 
                                data-side-pagination="server"
                                data-page-list="[10, 20, 50, 100]"
                                data-page-size="20"  
                                data-query-params="queryParams">
                                <thead>
                                    <tr class="tab-th">
                                        <th data-field="comboId" class="col-md-1"></th>
                                        <th data-field="comboTitle" class="col-md-4" data-formatter="ToRole" >套餐标题</th>
                                        <th data-field="createTime" class="col-md-3" data-formatter="timeFormatter" class="col-md-2">创建时间</th>
                                        <th data-field="comboStatus"  class="col-md-2" data-formatter="statusFormatter">状态</th>
                                        <th data-field="action" data-formatter="actionFormatter" class="col-md-2">操作</th>
                                    </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
        </div>
       
    

    <!-- 引入所有joli的脚本 -->
    <jsp:include page="../joli/scripts.jsp"></jsp:include>
    <!-- 引入页面需要的脚本 -->
    <script type="text/javascript" src="${ctx}/rs/js/bootstrapQ.min.js"></script>
    <script src="${ctx}/rs/js/bootstrap-table.js"></script>
    <script src="${ctx}/rs/js/bootstrap-table-zh-CN.js"></script>
    <script src="${ctx}/rs/js/jquery.formatDate.js"></script>
    <script src="${ctx}/rs/js/my97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="${ctx}/rs/js/session-out-error.js"></script>
    <script>
    
      activeMenu($("#menu_setMeal_0"));
      var $table = $('#table');
      
      $table.on('click-row.bs.table', function (e, row, $element) {
            $table.bootstrapTable('uncheckAll');
            $table.bootstrapTable('check', $element.index());
      });
      
      $table.bootstrapTable({
          height: getHeight()
      });
        
      //获取分辨率高度
        function getHeight() {
            return window.screen.height - 308;
        }
        
        $(window).resize(function () {
        $table.bootstrapTable('resetView',{height: getHeight()});
      });
        
        //初始化刷新
         function refresh(pageIndex,selectPage){
        	var comboStatus = $("#comboStatus").val();
             var comboTitle = $("#comboTitle").val();
            if(!pageIndex && !selectPage){
                pageIndex = 0;
                selectPage = 0;
            }
            var pageSize = $table.bootstrapTable('getOptions').pageSize;
            pageIndex=pageIndex*pageSize;
            $table.bootstrapTable('refresh', {
                silent: true,
                cache : false,
                url: '${ctx}/TblComboInfo/page.htm',
                query: {
                	comboStatus : comboStatus, 
                	comboTitle :encodeURIComponent(comboTitle),
                    pageIndex : pageIndex,
                    pageSize : pageSize
                }
           });
            $table.bootstrapTable('selectPage', selectPage);
        }
        function queryParams(params) {
        	var comboStatus = $("#comboStatus").val();
            var comboTitle = $("#comboTitle").val();
            return {
           	    comboStatus : comboStatus,
           	    comboTitle :encodeURIComponent(comboTitle),
                pageIndex : params.offset,
                pageSize : params.limit
            };
        }
        function timeFormatter(value, row, index){
             if(value){
                 return $.formatDate("yyyy-MM-dd HH:mm", new Date(value));
             }
             return "--";
        }
        function actionFormatter(value, row, index){
        	var  bak  = '';
        	if(row.comboStatus == "1"){//执行中
        		bak  =    '<a class="mr15" href="javascript:detail('+row.comboId+')">查看</a><a class="mr15" href="javascript:update('+row.comboId+',2)">关闭</a>';
        	}else if(row.comboStatus == "2"){//已关闭
        		bak  =    '<a class="mr15" href="javascript:detail('+row.comboId+')">查看</a><a class="mr15" href="javascript:update('+row.comboId+',1)">开启</a><a class="mr15" href="javascript:update('+row.comboId+',-1)">删除</a>';
        	}else if(row.comboStatus == "0"){//草稿
        		bak  =    '<a class="mr15" href="javascript:edit('+row.comboId+')">编辑</a><a class="mr15" href="javascript:update('+row.comboId+',1)">开启</a><a class="mr15" href="javascript:update('+row.comboId+',-1)">删除</a>';
        	}
        	return bak;
        }
        function statusFormatter(value,index,row){
        	var bak = '';
        	if(value == "1"){
        		bak = "执行中";
        	}else if(value == "0"){
        		bak = "草稿";
        	}else if(value == "2"){
                bak = "已关闭";
            }
        	return bak;
        }
 
        function update(id,status){
             var pageNumber = $table.bootstrapTable('getOptions').pageNumber;
             var pageIndex = pageNumber - 1;
               
            if(status==1){
                bootstrapQ.confirm('确定开启该套餐?',function(){
                    $.ajax({
                       url: "${ctx}/TblComboInfo/update.htm",
                      type: 'POST',
                      data: {
                          id:id,
                          status:status
                      },
                        success: function (text) {
                            bootstrapQ.msg({
                                msg  : '操作成功！',
                                type : 'success',
                                time : 2000
                            });
                            refresh(pageIndex,pageNumber);
                        },
                        error: function () {
                            bootstrapQ.msg({
                                msg  : '操作失败！',
                                type : 'danger',
                                time : 2000
                            });
                            refresh(pageIndex,pageNumber);
                        }
                    });
                },null);
            }else if(status==2){
            	bootstrapQ.confirm('确定关闭该套餐?',function(){
                      $.ajax({
                           url: "${ctx}/TblComboInfo/update.htm", 
                          type: 'POST',
                          data: {
                              id:id,
                              status:status
                          },
                            success: function (text) {
                                bootstrapQ.msg({
                                    msg  : '操作成功！',
                                    type : 'success',
                                    time : 2000
                                });
                                setTimeout("window.location.reload(true)",2000);
                            },
                            error: function () {
                                bootstrapQ.msg({
                                    msg  : '操作失败！',
                                    type : 'danger',
                                    time : 2000
                                });
                                setTimeout("window.location.reload(true)",1000);
                            } 
                     }); 
                  });
            }else if(status==-1){
            	var rows = $table.bootstrapTable('getSelections');
            	if(rows[0].planStatus == null ){
            		bootstrapQ.confirm('确定删除该套餐?',function(){
                        $.ajax({
                             url: "${ctx}/TblComboInfo/update.htm", 
                            type: 'POST',
                            data: {
                                id:id,
                                status:status
                            },
                              success: function (text) {
                                  bootstrapQ.msg({
                                      msg  : '操作成功！',
                                      type : 'success',
                                      time : 2000
                                  });
                                  setTimeout("window.location.reload(true)",2000);
                              },
                              error: function () {
                                  bootstrapQ.msg({
                                      msg  : '操作失败！',
                                      type : 'danger',
                                      time : 2000
                                  });
                                  setTimeout("window.location.reload(true)",1000);
                              } 
                       }); 
                    });
            	
            	} else if( rows[0].planStatus.indexOf("0") != -1|| rows[0].planStatus.indexOf("2") !=-1 || rows[0].planStatus.indexOf("3") !=-1|| rows[0].planStatus.indexOf("4") !=-1|| rows[0].planStatus.indexOf("5") !=-1 ){
            		bootstrapQ.msg({
                        msg  : '不能删除未执行完的套餐！',
                        type : 'danger',
                        time : 2000
                    });
          }else {
        	  bootstrapQ.confirm('确定删除该套餐?',function(){
                  $.ajax({
                       url: "${ctx}/TblComboInfo/update.htm", 
                      type: 'POST',
                      data: {
                          id:id,
                          status:status
                      },
                        success: function (text) {
                            bootstrapQ.msg({
                                msg  : '操作成功！',
                                type : 'success',
                                time : 2000
                            });
                            setTimeout("window.location.reload(true)",2000);
                        },
                        error: function () {
                            bootstrapQ.msg({
                                msg  : '操作失败！',
                                type : 'danger',
                                time : 2000
                            });
                            setTimeout("window.location.reload(true)",1000);
                        } 
                 }); 
              });
        	  
          }
          }
        }
        
        function detail(id) {
        	var biaoshi = 1;
                bootstrapQ.dialog({
                    url : "${ctx}/TblComboInfo/detail.htm?id="+id+"&biaoshi="+biaoshi,
                    title : "套餐详细信息",
                    width: '720px',
                    close : 'true',
                    hidebtn : 'true'
                },function(){
                    //回调触发关闭窗口
                    $("#bsmodal").modal("hide");
                });
        }
        function add() {
            var pageNumber = $table.bootstrapTable('getOptions').pageNumber;
            var pageIndex = pageNumber - 1;
            bootstrapQ.dialog({
                url : '${ctx}/TblComboInfo/add.htm',
                title : '新建套餐',
                width: "850px",
                close : 'true',
                hidebtn : 'true'
            },function(){
                //回调触发关闭窗口
                $("#bsmodal").modal("hide");
                refresh(pageIndex,pageNumber);
            });
        };
        function edit(id) {
            var pageNumber = $table.bootstrapTable('getOptions').pageNumber;
            var pageIndex = pageNumber - 1;
            var biaoshi = 2;
            bootstrapQ.dialog({
                url : "${ctx}/TblComboInfo/detail.htm?id="+id+"&biaoshi="+biaoshi,
                title : '编辑套餐',
                width: "850px",
                close : 'true',
                hidebtn : 'true'
            },function(){
                //回调触发关闭窗口
                $("#bsmodal").modal("hide");
                refresh(pageIndex,pageNumber);
            });
        };
        function removeData() {
            var pageNumber = $table.bootstrapTable('getOptions').pageNumber;
            var pageIndex = pageNumber - 1;
            var rows = $table.bootstrapTable('getSelections');
            var len=$("#table tbody").find("tr").length;
            if(len==rows.length){
                pageIndex = pageIndex - 1;
            }
            if(rows.length > 0){
                bootstrapQ.confirm('确定删除选中记录？',function(){
                    var ids = $.map($table.bootstrapTable('getSelections'), function (row) {
                        return row.id;
                    });
                    $.ajax({
                        url: "${ctx}/message/delete.htm?ids="+ids,
                        success: function (text) {
                            bootstrapQ.msg({
                                msg  : '删除成功！',
                                type : 'success',
                                time : 2000
                            });
                            refresh(pageIndex,pageNumber);
                        },
                        error: function () {
                            bootstrapQ.msg({
                                msg  : '删除失败！',
                                type : 'danger',
                                time : 2000
                            });
                            refresh(pageIndex,pageNumber);
                        }
                    });
                },null);
            }else{
                bootstrapQ.alert("请选择要删除的记录！");
            }
        }

    </script>
</body>
</html>